package com.test.springCloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.test.springCloud.mapper.MsgInfoMapper;
import com.test.springCloud.mapper.SessionListMapper;
import com.test.springCloud.service.MsgInfoService;
import com.test.springCloud.utils.ResultUtils;
import com.test.springcloud.pojo.MsgInfoPojo;
import com.test.springcloud.pojo.SessionListPojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 信息发送接收
 */
@CrossOrigin  //跨域
@RequestMapping("/lxf")
@Slf4j
@RestController
public class MsgInfoController {

    @Resource
    private MsgInfoService msgInfoService;

    @Resource
    private SessionListMapper sessionListMapper;

    /**
     * // 消息列表
     * @param req
     * @return
     */
    @GetMapping("/msgList")
    public ResultUtils msgList(@RequestBody JSONObject req){

        Integer sessionId = req.getInteger("sessionId");
        SessionListPojo sessionList = sessionListMapper.selectByPrimaryKey(sessionId);
        if(sessionList == null){
            return ResultUtils.getSuccess("success");
        }
        Integer fromUserId = sessionList.getUserId();
        Integer toUserId = sessionList.getToUserId();
        List<MsgInfoPojo> msgInfoList = msgInfoService.selectMsgList(fromUserId,toUserId);
        // 更新消息已读
        msgInfoService.msgRead(fromUserId, toUserId);
        // 更新会话里面的未读消息
        sessionListMapper.delUnReadCount(fromUserId, toUserId);
        return ResultUtils.getSuccess("success",msgInfoList);
    }
}
